<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size:18px ">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-promotion"></i> 后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加相册</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider style="margin-bottom: 50px"></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="简 介" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">退出</el-button>
      </el-form-item>
    </el-form>


  </div>
</template>

<script>
export default {
  name: "AlbumAddNewView",
  data() {
    return {
      ruleForm: {
        name: '',
        description: '',
        sort: ''
      },
      rules: {
        /*trigger表示检查什么情况会触发，blur表示当控件丢失焦点时触发检查*/
        name: [
          {required: true, message: '请输入相册名称', trigger: 'blur'},
          {min: 2, max: 15, message: '相册必须在 2 到 15 个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入相册简介', trigger: 'blur'},
          {min: 2, max: 35, message: '相册简介必须在 2 到 35 个字符', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入相册排序序号', trigger: 'blur'},
          {pattern: /^([0-9]{1}|[1-9]{1}[0-9]{1})$/, message: '排序序号必须在 0 到 99 数字之间', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "http://localhost:15722/albums/add-new";
          let formData =this.qs.stringify(this.ruleForm);
          /*let formData = 'name=' + this.ruleForm.name
              +'&description=' + this.ruleForm.description
              + '&sort=' + this.ruleForm.sort;*/
          console.log('URL: '+url);
          console.log('参数: '+formData);
          /*在vue cli框架中不能携程 function(response)格式 只能携程then((response)=>{ 方法体 })*/
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) =>{
            /*在整个response中是包含了很多很多的一些列的信息*/
            /*response的data属性存放的就是服务端响应的具体信息*/
            let jsonResult = response.data;
            console.log(jsonResult.state);
            if (jsonResult.state == 20000){
              this.$message({
                message: '添加相册成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$alert(jsonResult.message, '操作失败', {
                confirmButtonText: '确定',
                callback: action => { //callback当点击关闭时触发，暂时无需求
                }
              });
            }
            /*console.log(response.data);*/
          }) ;
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>